package io.renren.modules.app.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.entity.MemberPurseEntity;
import io.renren.modules.app.entity.TradeRecordingEntity;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.entity.vo.QueryWithdrawalsParam;
import io.renren.modules.app.entity.vo.SelectWithdrawalsData;
import io.renren.modules.app.service.MemberPurseService;
import io.renren.modules.app.service.TradeRecordingService;
import io.renren.modules.app.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.app.entity.WithdrawalsRecordingEntity;
import io.renren.modules.app.service.WithdrawalsRecordingService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 提现记录表
 *
 * @date 2022-04-20 22:37:44
 */
@RestController
@RequestMapping("app/withdrawalsrecording")
public class WithdrawalsRecordingController {

    @Autowired
    private WithdrawalsRecordingService withdrawalsRecordingService;

    @Autowired
    private UserService userService;

    @Autowired
    private MemberPurseService memberPurseService;

    @Autowired
    private TradeRecordingService tradeRecordingService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:withdrawalsrecording:list")
    public R list(QueryWithdrawalsParam withdrawalsParam) {
        SelectWithdrawalsData withdrawalsData = withdrawalsRecordingService.queryWithdrawalsRecording(withdrawalsParam);
        return R.ok().put("page", withdrawalsData);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{withdrawalsId}")
    @RequiresPermissions("generator:withdrawalsrecording:info")
    public R info(@PathVariable("withdrawalsId") Long withdrawalsId) {
        WithdrawalsRecordingEntity withdrawalsRecording = withdrawalsRecordingService.getById(withdrawalsId);

        return R.ok().put("withdrawalsRecording", withdrawalsRecording);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:withdrawalsrecording:save")
    public R save(@RequestBody WithdrawalsRecordingEntity withdrawalsRecording) {
        withdrawalsRecording.setCreateTime(new Date());
        withdrawalsRecordingService.save(withdrawalsRecording);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:withdrawalsrecording:update")
    public R update(@RequestBody WithdrawalsRecordingEntity withdrawalsRecording) {
        withdrawalsRecordingService.updateById(withdrawalsRecording);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:withdrawalsrecording:delete")
    public R delete(@RequestBody Long[] withdrawalss) {
        withdrawalsRecordingService.removeByIds(Arrays.asList(withdrawalss));

        return R.ok();
    }

    /**
     * 分页获取提现记录
     *
     * @param withdrawalsParam 参数类
     * @return 提现记录
     */
    @Login
    @GetMapping("/queryWithdrawalsList")
    public R queryWithdrawalsList(QueryWithdrawalsParam withdrawalsParam) {
        SelectWithdrawalsData withdrawalsData = withdrawalsRecordingService.queryWithdrawalsRecording(withdrawalsParam);
        return R.ok().put("withdrawalsList", withdrawalsData);
    }

    /**
     * 新增提现记录
     *
     * @param withdrawalsRecording 提现信息类
     * @return
     */
    @Login
    @PostMapping("addWithdrawalsRecording")
    public R addWithdrawalsRecording(@RequestBody WithdrawalsRecordingEntity withdrawalsRecording) {
        if (withdrawalsRecording.getUserId() == null) {
            return R.error("用户id不能为空");
        }
        //获取用户钱包
        MemberPurseEntity purse = memberPurseService.queryUserPurse(withdrawalsRecording.getUserId());
        if (purse.getWithdrawalsAmount().compareTo(withdrawalsRecording.getAmount()) == -1) {
            return R.error("金额不足");
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(withdrawalsRecording.getUserId());
        UserEntity user = userService.queryUserDetails(userEntity);
        //传过来的交易密码
        String tradePass = DigestUtils.sha256Hex(withdrawalsRecording.getTradePass());
        //校验交易密码是否正确
        if (!user.getTradePass().equals(tradePass)) {
            return R.error("交易密码错误，请重新输入");
        }
        //提现手续费金额(提现金额 * 提现费率)
        BigDecimal amount = withdrawalsRecording.getAmount();
        BigDecimal premium = new BigDecimal(withdrawalsRecording.getPremium());
        //提现手续费金额
        BigDecimal withdrawalFeeAmount = amount.multiply(premium);

        //实际到账金额= 提现金额-提现手续费金额(提现金额 * 提现费率)
        withdrawalsRecording.setArrivalAmount(withdrawalsRecording.getAmount().subtract(withdrawalFeeAmount));
        Integer integer = withdrawalsRecordingService.addWithdrawalsRecording(withdrawalsRecording);
        if (integer > 0) {

            MemberPurseEntity memberPurse = new MemberPurseEntity();
            memberPurse.setUserId(withdrawalsRecording.getUserId());
            memberPurse.setWithdrawalsAmount(purse.getWithdrawalsAmount().subtract(withdrawalsRecording.getAmount()));
            //扣取提现的金额
            memberPurseService.editMemberPurse(memberPurse);

            TradeRecordingEntity tradeRecording = new TradeRecordingEntity();
            tradeRecording.setUserId(withdrawalsRecording.getUserId());
            tradeRecording.setTradeAmount(withdrawalsRecording.getAmount());
            tradeRecording.setTradeType(1);
            //新增交易记录
            tradeRecordingService.addTradeRecording(tradeRecording);
            return R.ok();
        } else {
            return R.error("提现失败");
        }
    }

}
